package com.geek.novel.activity.base

import android.graphics.drawable.ColorDrawable
import android.text.Editable
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import com.geek.novel.R
import com.geek.novel.utils.ToastUtils
import com.zackratos.ultimatebarx.ultimatebarx.addStatusBarTopPadding
import com.zackratos.ultimatebarx.ultimatebarx.statusBarHeight
import kotlinx.android.synthetic.main.activity_title_bar.*


/**
 * 标题栏的Activity
 */
abstract class TitleBarActivity : BaseActivity() {

    abstract fun pageName(): String

    /**
     * 重写setContentView方法，填充TitleBar页面的布局，然后引入子布局
     */
    override fun setContentView(layoutResID: Int) {
        super.setContentView(R.layout.activity_title_bar)
	
		//添加沉浸式状态栏的高度
		ly_title_bar.addStatusBarTopPadding()
		
		//填充容器顶部的占位块的高度
		setViewContainerFill(view_container_offset_block)
		
        //顶部字体加粗
        tv_title_view_text.paint.isFakeBoldText = true
        //设置标题
        setTitle(pageName())

        //填充子布局
        LayoutInflater.from(this).inflate(layoutResID, ly_title_view_container)

        //绑定事件
        bindTitleBarEvent()

        //附加控件
        this.attachViewChild()

        //绑定事件
        this.bindEventChild()
    }

    /**设置标题*/
    protected fun setTitle(title: String){
        tv_title_view_text.text = title
    }

    /**显示标题右侧按钮*/
    protected fun showTitleBtn(text: String){
        btn_title_view_setting.visibility = View.VISIBLE
        updateTitleBtnText(text)
        btn_title_view_setting.setOnClickListener{
            this.onTitleBtnClick(it)
        }
    }

    /**显示标题右侧按钮*/
    private fun showTitleBtnAsSearch(){
        btn_title_view_setting.visibility = View.VISIBLE
        updateTitleBtnText("搜索")
        btn_title_view_setting.setOnClickListener{
            val text = btn_title_view_search.text
            this.onSearchBtnClick(it, text)
        }
    }

    /**设置搜索框文本的placeholder*/
    open fun setSearchHint(hint: String){
        btn_title_view_search.hint = hint
    }

    /**设置搜索框文本*/
    open fun setSearchText(text: String){
        btn_title_view_search.setText(text)
    }

    /**更新标题文本*/
    protected fun updateTitleBtnText(text: String){
        btn_title_view_setting.text = text
    }

    /**显示搜索框*/
    protected fun showSearchBox(){
        btn_title_view_search.visibility = View.VISIBLE
        this.showTitleBtnAsSearch()
        //添加回车事件发起搜索
        btn_title_view_search.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH){
                val text = btn_title_view_search.text
                this.onSearchBtnClick(v, text)
            }
            true
        }
    }

    /**显示标题右侧的图标按钮*/
    protected fun showIconBtn(resId: Int){
        btn_title_view_icon.visibility = View.VISIBLE
        this.updateIconBtn(resId)
        btn_title_view_icon.setOnClickListener{
            this.onIconBtnClick(it)
        }
    }

    /**设置边距*/
    protected fun setIconBtnPadding(padding: Int){
        btn_title_view_icon.setPadding(padding, padding, padding, padding)
    }

    /**隐藏标题右侧的图标按钮*/
    protected fun hideIconBtn(){
        btn_title_view_icon.visibility = View.GONE
    }

    /**更新标题右侧的图标按钮*/
    private fun updateIconBtn(resId: Int){
        btn_title_view_icon.setImageDrawable(this.getDrawable(resId))
    }


    /**设置背景颜色*/
    protected open fun setBackgroundColor(color: Int){
        layout_title_bar_container.setBackgroundColor(color)
    }

    /**设置标题栏目背景透明*/
    protected open fun setActionBarTransparent(color: Int){
        //设置一张透明的白色背景
        setActionBarBackground(color, 0F)
        //将返回按钮颜色变成黑色
        btn_title_view_back.setImageDrawable(getDrawable(R.drawable.icon_back_black))
        tv_title_view_text.setTextColor(getColor(R.color.c333))
        btn_title_view_setting.setTextColor(getColor(R.color.c333))
    }

    /**设置标题背景的透明度*/
    protected open fun setActionBarBackground(color: Int, alpha: Float){
        ly_title_bar.background = ColorDrawable(color)
        setActionBarBackgroundAlpha(alpha)
    }

    /**设置标题背景的透明度*/
    protected open fun setActionBarBackgroundAlpha(alpha: Float){
        val result = this.calcAlpha(alpha)
        ly_title_bar.background.alpha = result
    }

    /**设置标题文字的透明度*/
    protected open fun setActionBarTitleAlpha(alpha: Float){
        tv_title_view_text.alpha = alpha
    }

    /**alpha转255*/
    private fun calcAlpha(alpha: Float): Int{
        var result = (255 * alpha).toInt()
        result = result.coerceAtLeast(0)
        result = result.coerceAtMost(255)
        return result
    }

	/**隐藏掉填充容器顶部的占位块*/
	protected open fun hideContainerBlock(){
		view_container_offset_block.visibility = View.GONE
	}
	
    /**设置容器为填充高度状态，前提是必须有个view设置为padding-top用于占据标题栏的高度*/
    protected open fun setViewContainerFill(containerView: View?){
        if(containerView != null){
            val left = containerView.paddingLeft
            val top = containerView.paddingTop
            val right = containerView.paddingRight
            val bottom = containerView.paddingBottom
            //顶部边距添加标题栏的高度
            val actionBarHeight = resources.getDimension(R.dimen.action_bar_height).toInt()
	
            val offsetTop = top + statusBarHeight + actionBarHeight
            containerView.setPadding(left, offsetTop, right, bottom)
        }
    }

    /**获取标题栏的高度*/
    protected open fun getActionBarHeight(): Int{
        return ly_title_bar.height
    }

    /**标题右侧按钮的点击事件*/
    protected open fun onTitleBtnClick(view: View){
    }

    /**标题右侧的搜索按钮点击事件*/
    protected open fun onSearchBtnClick(view: View, text: Editable){
    }

    /**标题右侧图标按钮的点击事件*/
    protected open fun onIconBtnClick(view: View){
    }

    /**
     * 绑定事件
     */
    private fun bindTitleBarEvent() {
        //后退按钮点击事件，直接关闭activity
        btn_title_view_back.setOnClickListener {
            finish()
        }
    }

    /**附加控件*/
    protected open fun attachViewChild() {}

    /**绑定事件*/
    protected open fun bindEventChild() {}

}